import list from './list'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nowTime: "",
    list,
    title: "",
    content: "",
    author: ""
  },
  inputTitle(e) {
    // console.log(e.detail.value);
    this.setData({
      title: e.detail.value
    })
    // console.log(this.data.title);
  },
  inputContent(e) {
    // console.log(e.detail.value);
    this.setData({
      content: e.detail.value
    })
    // console.log(this.data.content);
  },
  inputAuthor(e) {
    // console.log(e.detail.value);
    this.setData({
      author: e.detail.value
    })
    // console.log(this.data.author);
  },
  btnClick() {
    let that = this
    if (that.data.author.trim() == "" || that.data.title.trim() == "" || that.data.content.trim() == "") {
      wx.showToast({
        title: '不能为空',
        icon: 'error',
        duration: 2000
      })
    } else {
      const list2 = {
        id: that.data.list.length + 1,
        author: that.data.author.trim(),
        title: that.data.title.trim(),
        content: that.data.content.trim(),
        collection: false,
        timeStamp: that.getNowTime()
      };
      const list3 = [...that.data.list, list2];
      // console.log(list3);
      that.setData({
        list: list3,
        nowTime: that.getNowTime(),
        author:"",
        title:"",
        content:""
      })
      // console.log(that.data.list);
    }
    setInterval(() => {
      this.setData({
        nowTime: that.getNowTime()
      })
    }, 60000)


  },
  collection(e) {
    // console.log(e.target.dataset.index);
    let index = e.target.dataset.index
    let list1 = this.data.list;
    // console.log(list1[index].collection);
    list1[index].collection = !list1[index].collection
    // console.log(list1[index].collection);
    this.setData({
      list: list1
    })
  },
  delete(e) {
    // console.log(e.target.dataset.index);
    let index = e.target.dataset.index;
    let list2 = this.data.list
    list2.splice(index, 1)
    // console.log(list2);
    this.setData({
      list:list2
    })
    // if (list2.length > 1) {
    //   this.setData({
    //     list: list2.splice(index - 1, 1)
    //   })
    // } else {
    //   this.setData({
    //     list: []
    //   })
    // }

  },
  //  bar(change){
  //     let  myDate = new Date();
  //     let time = myDate.getTime();
  //     console.log(time);
  //     let hour = 360000;
  //     let timeRemaining = time - change;
  //     let hourRemaining = parseInt(timeRemaining/hour);
  //     console.log(hourRemaining);
  //   },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //  this.bar(1625032954251);
    // console.log(this.data.list2);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getNowTime() {
    let myDate = new Date();
    return myDate.getTime();
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    this.setData({
      nowTime: that.getNowTime()
    })
    setInterval(() => {
      this.setData({
        nowTime: that.getNowTime()
      })
    }, 60000)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})